package com.edu.springboot_disk_erban.service.Impl;

import com.edu.springboot_disk_erban.dao.FileDao;
import com.edu.springboot_disk_erban.dao.UserDao;
import com.edu.springboot_disk_erban.entity.FileBean;
import com.edu.springboot_disk_erban.entity.User;
import com.edu.springboot_disk_erban.handler.HDFSHandler;
import com.edu.springboot_disk_erban.service.UserService;
import com.edu.springboot_disk_erban.utils.HDFSConnectUtils;
import com.edu.springboot_disk_erban.utils.TimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserDao userdao;

    @Resource
    FileDao fileDao;

    @Resource
    HDFSHandler handler;
    @Override
    public User findUser(User user) {
        User findUser = userdao.findUser(user);

        return findUser;
    }

    @Override
    public int registry(User user) {
        //操作dao层，在数据库里查询用户名是否存在
        User userByUserName = userdao.findUserByUserName(user.getUsername());
        int num;
        //存在的话
        if (userByUserName !=null){
            num = 0;

        }else {
            //用户不存在，调用dao层添加用户
            //还需要在HDFS 上创建对应的文件夹 且 还要把创建的文件夹记录在MySQL中fileBean中
            String username = user.getUsername();
            String path ="/" +username;

            num = userdao.addUser(user);

            FileBean fileBean = new FileBean(username,path,"0", TimeUtils.getNowTime(),1);
            fileDao.addFileBean(fileBean);

            try {
                handler.addDir(path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return num;
    }

    public User findUserInfo(String username){
        User user = userdao.findUserInfo(username);
        return user;
    }

    public void editUserInfo(User user){
        userdao.editUserInfo(user);
    }
//
    public void editPasswd(User user){
        userdao.editPasswd(user);
    }
}
